package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Room;
import com.gaohe.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/room")
@Slf4j
public class RoomController {
    @Autowired
    private RoomService roomService;
    //教室分页查询
    @GetMapping("/page")
    public R page(
            @RequestParam int page,
            @RequestParam int pageSize,
            @RequestParam(required = false) String name){
        Page<Room> roomPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Room> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Room::getName,name);
        qw.orderByDesc(Room::getUpdateTime);
        roomService.page(roomPage,qw);
        return R.success(roomPage);

    }
    //添加教室
    @PostMapping
    public R save(@RequestBody Room room){
        boolean save = roomService.save(room);
        return save?R.success("新增成功"):R.error("新增失败");
    }
    //教室信息修改
    //回显
    @GetMapping("/{id}")
    public R findByid(@PathVariable Long id) {
        return R.success(roomService.getById(id));
    }
    //修改
    @PutMapping
    public R update(@RequestBody Room room)
    {
        boolean b = roomService.updateById(room);
        return b?R.success("修改成功"):R.error("修改失败");
    }
    //教室删除
    @DeleteMapping
    public R delete(@RequestParam Long id){
        boolean b = roomService.removeById(id);
        return b?R.success("删除成功"):R.error("删除失败");
    }
    @GetMapping
    public R findAll(){
        return R.success(roomService.list());
    }
}
